package org.nnxy.foodboot.controller;

import org.nnxy.foodboot.common.Result;
import org.nnxy.foodboot.entity.Recipe;
import org.nnxy.foodboot.service.PopularRecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 热门食谱控制器
 */
@RestController
@RequestMapping("/popular")
public class PopularRecipeController {
    
    @Autowired
    private PopularRecipeService popularRecipeService;
    
    /**
     * 获取热门食谱列表
     * @param limit 限制条数
     * @return 热门食谱列表
     */
    @GetMapping("/recipes")
    public Result<List<Recipe>> getPopularRecipes(@RequestParam(defaultValue = "10") Integer limit) {
        List<Recipe> recipes = popularRecipeService.getPopularRecipes(limit);
        return Result.success(recipes);
    }
    
    /**
     * 更新食谱的热门指标
     * @param recipeId 食谱ID
     * @param viewIncrement 浏览增量
     * @param likeIncrement 点赞增量
     * @param collectIncrement 收藏增量
     * @return 操作结果
     */
    @PostMapping("/update-metrics")
    public Result<Void> updateRecipeMetrics(
            @RequestParam Long recipeId,
            @RequestParam(required = false, defaultValue = "0") Integer viewIncrement,
            @RequestParam(required = false, defaultValue = "0") Integer likeIncrement,
            @RequestParam(required = false, defaultValue = "0") Integer collectIncrement) {
        
        popularRecipeService.updateRecipePopularityMetrics(recipeId, viewIncrement, likeIncrement, collectIncrement);
        return Result.success();
    }
    
    /**
     * 刷新热门食谱排名
     * @return 操作结果
     */
    @PostMapping("/refresh-ranks")
    public Result<Void> refreshPopularRanks() {
        popularRecipeService.refreshPopularRecipeRanks();
        return Result.success();
    }
} 